package sensor;

import java.util.ArrayList;
import java.util.List;

public abstract class Sensor implements Subject {
    private List<Observer> observers = new ArrayList<>();
    private String sensorData;

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(sensorData);
        }
    }

    public void setSensorData(String data) {
        this.sensorData = data;
        notifyObservers();
    }

    // 通知所有观察者，接收 double 类型参数
    public abstract void notifyObservers(double data);

    // 新增：通知所有观察者，接收 String 类型参数
    public void notifyObservers(String data) {
        for (Observer observer : observers) {
            observer.update(data);
        }
    }
}